/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.business.MessageManager;
import giuliani_mapelli_marocco.entity.Notification;
import giuliani_mapelli_marocco.enumerations.MessageType;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class NotificationBean implements Serializable{
    
    @EJB
    UserManager um;
    
    @EJB
    MessageManager mm;
    
    private List<Notification> notificationList;

    /**
     * Creates a new instance of NotificationBean
     */
    public NotificationBean() {
    }
    
    /**
     * Initializes the notification panel, adding all the notifications that the user have received
     */
    @PostConstruct
    public void init() {
        notificationList = mm.getAllUserNotification(um.getLoggedUser());
    }
    
    public List<Notification> getNotificationList() {
        return notificationList;
    }

    public void setNotificationList(List<Notification> notificationList) {
        this.notificationList = notificationList;
    }
    
    /**
     * Deletes the notification
     * @param n the notification to delete
     */
    public void deleteNotification(Notification n) {
        if(n == null)
            return;
        Long id = n.getId();
        notificationList.remove(n);
        mm.deleteNotification(id);
    }

    /**
     * Method that tells whether the logged user can accept or decline the invitation
     * @param n the invitation-notification
     * @return true if the logged user can accept or decline, otherwise returns false
     */
    public boolean canAcceptOrDecline(Notification n) {
        return n.getIdEvent().getUserCollection().contains(um.getLoggedUser()) && n.getType().equals(MessageType.INVITATION);
    }
    
    /**
     * Method that tells whether you can delete the notification or not
     * @param n the notification
     * @return true if the user can delete the notification
     */
    public boolean canDeleteNotification(Notification n) {
        MessageType nType = n.getType();
       return (nType.equals(MessageType.DELETION) || nType.equals(MessageType.MODIFICATION));
   }
   
    /**
     * Tells whether you can see details of the event associated to the notification or not
     * @param n the notification
     * @return true if you can see the details of the event
     */
    public boolean canSeeDetails(Notification n) {
        return !(n.getType().equals(MessageType.DELETION));
    }
   
   /**
    * Get the message of the notification to display
    * @param n the notification
    * @return the message
    */
    public String getMessage(Notification n) {
        String message;
        switch(n.getType()) {
            case INVITATION:
                message = n.getIdEvent().getOrganizer().getUsername() + " has invited you";
                break;
            case MODIFICATION:
                message = n.getIdEvent().getOrganizer().getUsername() + " has modified the event";
                break;
            case DELETION:
                message = n.getIdEvent().getOrganizer().getUsername() + " has deleted the event";
                break;
            case TIME_CONSISTENCY_ALERT:
                message = n.getIdEvent().getOrganizer().getUsername() + " has modified the event. You have time inconsistencies. Please reconfirm your partecipation";
                break;
            default:
                message = null;
        }
        return message;
   }
}
